package com.gr.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.entity.Resource;
import com.gr.entity.ResultEntity;
import com.gr.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 * @author guoran
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IResourceService resourceService;
    @GetMapping("/showUserMenu")
    public ResultEntity<List<Resource>> selectLeftMenu(){
        // toDo 动态获取用户id 先写死
        int userId=3;
        List<Resource> resources = resourceService.selectResourcesByAdminId(userId);
        return ResultEntity.ok(resources);//封装
    }

    @GetMapping("/showAllResourceTree")
    public ResultEntity<List<Resource>> showAllResourceTree(){
        //获取权限树
        //1.先获取所有的权限信息,一级权限、二级权限、三级权限
        List<Resource> allResource = resourceService.list();//无分层
        //从所有的权限里面过滤出来一级权限
        List<Resource> firstLevelResource = allResource.stream().filter((resource -> {
            return resource.getParentId() == 0;
        })).peek((resource)->{
            //根据父菜单的id,找到该父菜单对应的子菜单，单独定义出来一个方法
            List<Resource> children = getChildrenByPid(resource.getMenuId(), allResource);
            resource.setResourceList(children);//给一级菜单对应的二级菜单赋值
        }).collect(Collectors.toList());//集合只有一级权限,要有二级权限
        return ResultEntity.ok(firstLevelResource);
    }
    //根据父菜单的id，来找父菜单下面的子菜单
    private List<Resource> getChildrenByPid(long pid,List<Resource> allResource){//寻找子节点
        return allResource.stream().filter((resource) -> {
            return resource.getParentId().equals(pid);//小坑
        }).peek((resource)->{
            //想办法找到resource对象的孩子就好了list集合放入参数
            List<Resource> list = getChildrenByPid(resource.getMenuId(), allResource);//递归，会依次找孩子，循环到没有孩子
            resource.setResourceList(list);//给二级菜单对应的三级菜单赋值
        } ).collect(Collectors.toList());
    }

    @GetMapping("/selectAll")
    public ResultEntity<List<Resource>> selectAll(){
        List<Resource> list = resourceService.list();
        return ResultEntity.ok(list);
    }

    @GetMapping("/selectOne/{resourceId}")
    public ResultEntity<Resource> selectOne(@PathVariable("resourceId")int resourceId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("menu_id",resourceId);
        Resource one = resourceService.getOne(wrapper);
        return ResultEntity.ok(one);
    }

    @PutMapping("/update")
    public ResultEntity update(@RequestBody Resource resource){
        resourceService.updateById(resource);
        return ResultEntity.ok();
    }

    @GetMapping("/selectChildrenByPid/{resourceId}")
    public ResultEntity<List<Resource>> selectChildrenByPid(@PathVariable("resourceId")Integer resourceId){
        //传入menuId 根据menuID查对应ParentID下的Resource
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",resourceId);
        List<Resource> list = resourceService.list(wrapper);
        return ResultEntity.ok(list);
    }
}

